﻿define(function (require, exports, module) {
    var CPDF_Point = FXREADER.CPDF_Point;
    var CPDF_Rect = FXREADER.CPDF_Rect;
    var CommonTools = FXREADER.Common;
    FXREADER.PDFData.Text = {};

    var CHAR_INFO = function () {
        this.fCharRect = new CPDF_Rect(0, 0, 0, 0);
        this.charCode = -1;
    };
    FXREADER.PDFData.Text.CharInfo = CHAR_INFO;

    var CARET_INFO = function () {
        this.index = -1;
        this.bLeft = true;
    };
    FXREADER.PDFData.Text.CARET_INFO = CARET_INFO;

    FXREADER.PDFData.Text.CPDF_TextObject = function () {
        this.nObjIndex = 0; //Including the original characters as well as implicitly increase spaces
        this.nCharIndex = 0;    //All the characters, including the implicit increase spaces and line breaks
        this.nLineIndex = 0;
        this.charInfoArray = []; //All character information using Json Array to storage, for example, [left, top, width, height, charcode], must in PDF coordinates
        this.csText = "";   //Current text of a text object, through GetCharCodeText to get
        this.nStrSize = 0;
        this.bTrueText = false;
        var _cacheAllObjRect = null;
        this.Init = function () {
            this.csText = this.GetAllCharCodeText();
            this.nStrSize = this.csText.length;;//CommonTools.GetStringLength(this.csText); // this.csText.length;
            this.bTrueText = true;
        };

        this.GetSize = function () {
            return this.charInfoArray.length;
        };

        this.GetStrSize = function () {
            return this.nStrSize;
        };

        this.GetOBjectIndex = function (char_index) {
            var nItemSize = this.charInfoArray.length;
            if (char_index < 0 || char_index >= this.nStrSize || nItemSize == 0)
                return 0;
            var char_count = 0;
            for (var index = 0; index < nItemSize; index++) {
                var text = this.GetCharCodeText(index, 1);
                char_count += text.length;
                if (char_index + 1 <= char_count) {
                    return index;
                }
            }
            return 0;
        };

        this.GetObjectAllTextRect = function () {
            return this.GetObjectTextRect(0, this.charInfoArray.length);
        };

        this.GetObjectTextRect = function (start_Itemindex, char_count) {
            var charInfoLength = this.charInfoArray.length;
            var text_rect = null;
            if (char_count <= 0 || charInfoLength == 0 || start_Itemindex + char_count - 1 < 0
                 || start_Itemindex + char_count - 1 >= charInfoLength) {
                text_rect = new CPDF_Rect(0, 0, 0, 0);
                return text_rect;
            }
            text_rect = FXREADER.RectUtils.Clone(this.charInfoArray[start_Itemindex].fCharRect);
            for (var i = start_Itemindex + 1; i < start_Itemindex + char_count; i++) {
                var tmpRect = this.charInfoArray[i].fCharRect;
                FXREADER.RectUtils.Union(text_rect, tmpRect);
            }
            return text_rect;
        };

        this.GetAllObjectPDFRect = function (bClone)  //GetObjectPDFRect
        {
        	if(bClone == null)
        		bClone = true;
            if (!_cacheAllObjRect) {
                _cacheAllObjRect = this.GetObjectPDFRect(0, this.charInfoArray.length);
            }
            if(bClone)
            	return FXREADER.RectUtils.Clone(_cacheAllObjRect);
            else
            	return _cacheAllObjRect;
        };

        this.GetObjectPDFRect = function (start_charCode_index, char_count) {
            var charInfoLength = this.charInfoArray.length;
            if (start_charCode_index < 0 || start_charCode_index >= charInfoLength || charInfoLength == 0) {
                return new CPDF_Rect(0, 0, 0, 0);
            }
            if (start_charCode_index + char_count - 1 >= charInfoLength || char_count == -1) {
                char_count = charInfoLength - start_charCode_index;
            }
            if (start_charCode_index + char_count - 1 < 0 || start_charCode_index + char_count - 1 >= charInfoLength) {
                return new CPDF_Rect(0, 0, 0, 0);
            }

            var text_rect = this.GetObjectTextRect(start_charCode_index, char_count);
            return text_rect;
        };

        this.GetAllCharCodeText = function ()   //GetCharCodeText
        {
            return this.GetCharCodeText(0, this.charInfoArray.length);
        };

        this.GetCharCodeText = function (charCode_index, count) {
            var charInfoLength = this.charInfoArray.length;
            if (charCode_index < 0 || charCode_index >= charInfoLength)
                return "";
            if (this.bTrueText == true && charCode_index == 0 && count >= charInfoLength) {
                return this.csText;
            }
            if (charCode_index + count - 1 >= charInfoLength || count == -1) {
                count = charInfoLength - charCode_index;
            }

            var csText = "";
            var end_charCodeIndex = charCode_index + count - 1;
            for (var i = charCode_index; i <= end_charCodeIndex; i++) {
                var charinfo = this.charInfoArray[i];
                var chartext = String.fromCharCode(charinfo.charCode); //charinfo.m_CharCode;//this.Unicode2String(charinfo.m_CharCode); 
                csText += chartext;
            }

            return csText;
        };

        this.GetTextCaretInfo = function (point, caret_info) {
            if (this.charInfoArray.length == 0) {
                return false;
            }
            caret_info.index = -1;
            var caret_rect = new CPDF_Rect(0, 0, 0, 0);
            var objectrect = this.GetObjectAllTextRect();
            if (!FXREADER.RectUtils.PtInRect(objectrect, point)) {
                return false;
            }
            //[光标落在哪一个字符上]
            var nCount = this.charInfoArray.length;
            if (false) //m_bVertWriting
            {
                /*    for(int i=0; i<nCount; i++){		
                CPDF_Rect char_rect = GetObjectTextRect(i,1);			
                caret_rect=char_rect;
                if(char_rect.Contains(posx,posy)){ 		
                float n     = fixabs(posy-char_rect.top);
                float height = fixabs(char_rect.bottom-char_rect.top);			
                if(n>height/2){//[如果当前点在大部分落在左部,那caret的left和right都取左部(left);否则都取右部(right)]
                caret_rect.top=caret_rect.bottom=char_rect.bottom;
                caret_info.bLeft=FALSE;
                }
                else{
                caret_rect.top =caret_rect.bottom =char_rect.top;
                caret_info.bLeft=TRUE;
                }
                caret_info.caret_rect=caret_rect;
                caret_info.index=i;
                break;		
                }
                }*/
            }
            else {
                for (var i = 0; i < nCount; i++) {
                    var char_rect = this.GetObjectTextRect(i, 1);
                    if (FXREADER.RectUtils.PtInRect(char_rect, point)) {
                        var n = Math.abs(point.x - char_rect.left);
                        var width = Math.abs(char_rect.right - char_rect.left);
                        if (n > width / 2) {
                            //[如果当前点在大部分落在左部,那caret的left和right都取左部(left);否则都取右部(right)]
                            caret_info.bLeft = false;
                        }
                        else {
                            caret_info.bLeft = true;
                        }
                        caret_info.index = i;

                        break;
                    }
                }
            }
            //end
            if (caret_info.index != -1) {
                return true;
            }
            return false;
        };

        this.GetTextCaretInfoByIndex = function (item_index, caret_info, bLeft) {
            if (this.charInfoArray.length == 0) {
                return false;
            }
            //[光标落在哪一个字符上]
            if (item_index < 0 || item_index >= this.charInfoArray.length)
                return false;
            caret_info.bLeft = bLeft;
            caret_info.index = item_index;
            return true;
        };


    };

    return FXREADER.PDFData.Text.CPDF_TextObject;
});